package com.xc.service.impl;

import cn.hutool.core.lang.Assert;
import com.xc.config.JwtUtil;
import com.xc.dto.NavDTO;
import com.xc.entity.LoginEntity;
import com.xc.entity.NavEntity;
import com.xc.mapper.NavMapper;
import com.xc.service.NavService;
import com.xc.service.UserService;
import com.xc.dto.ResultDTO;
import com.xc.dto.UserDTO;
import com.xc.entity.UserEntity;
import com.xc.mapper.UserMapper;
import com.xc.util.JSONCopy;
import com.xc.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NavMapper navMapper;

    @Override
    public ResultDTO login(LoginEntity loginEntity) {
//        if (!userEntity.getCode().equals(userEntity.getVCode())) {
//        return ResultDTO.error("error-code");
//        }
        loginEntity.setPassword(MD5.encoderByMd5(loginEntity.getPassword()));
        UserDTO login = userMapper.login(loginEntity);
        if (login == null) {
            return ResultDTO.error("error");
        }
        List<NavDTO> navDTOList = navMapper.findNavAll(loginEntity);
        int count = navMapper.countNav(loginEntity);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        HashMap<String, Object> map = new HashMap<>();
//        map.put("name", loginEntity.getUsername());
        String jwt = JwtUtil.createJWT(uuid, "login subject", 0L, map);
//        map.put("data", navDTOList);
//        map.put("jwt", jwt);
//        链式调用
//        return ResultDTO.builder()
//                .count(count)
//                .code(200)
//                .msg(jwt)
//                .data(navDTOList)
//                .build();
        return ResultDTO.data(200, jwt, count, navDTOList);
    }

    @Override
    public ResultDTO login(UserEntity userEntity) {
        userEntity.setPassword(MD5.encoderByMd5(userEntity.getPassword()));
        UserEntity login = userMapper.wxLogin(userEntity);
        if (login == null) {
            int count = userMapper.add(userEntity);
            return ResultDTO.data(count > 0 ? "success" : "error", userMapper.wxLogin(userEntity));
        }
        return ResultDTO.data("success", login);

    }

    @Override
    public ResultDTO add(UserEntity userEntity) {
        Assert.notEmpty(userEntity.getUsername());
        userEntity.setPassword(MD5.encoderByMd5(userEntity.getPassword()));
        UserDTO byName = userMapper.findByName(userEntity);
        if (byName != null) {
            return ResultDTO.error("账号相同，请重写填写");
        }
        return ResultDTO.data(userMapper.add(userEntity), userMapper.findByName(userEntity));

    }

    @Override
    public ResultDTO update(UserEntity userEntity) {
        Assert.notEmpty(userEntity.getUsername());
        userEntity.setPassword(MD5.encoderByMd5(userEntity.getPassword()));
        return ResultDTO.data(userMapper.update(userEntity));

    }

    @Override
    public ResultDTO delete(UserEntity userEntity) {

        return ResultDTO.data(userMapper.delete(userEntity));
    }

    @Override
    public ResultDTO updateEnable(UserEntity entity) {

        return ResultDTO.data(userMapper.updateEnable(entity));
    }

    @Override
    public ResultDTO findById(UserEntity userEntity) {

        return ResultDTO.data(userMapper.findById(userEntity));
    }

    @Override
    public ResultDTO findAll(UserEntity userEntity) {
//        Map<String, Object> map = new TreeMap<>();
//        map.put("limit", userEntity.getLimit());
//        map.put("page", userEntity.getPage());
//        map.put("list", userMapper.findAll(userEntity));

        return ResultDTO.data(userMapper.count(userEntity), userMapper.findAll(userEntity));
    }

    @Override
    public ResultDTO count(UserEntity userEntity) {
        return ResultDTO.data(userMapper.count(userEntity));
    }

    @Override
    public ResultDTO findByName(UserEntity userEntity) {
        return null;
    }
}
